' Copyright 2012 Martin Leidel
'
' This software is provided 'as-is', without any express or implied
' warranty.  In no event will the authors be held liable for any damages
' arising from the use of this software.
' 
' Permission is granted to anyone to use this software for any purpose,
' including commercial applications, and to alter it and redistribute it
' freely, subject to the following restrictions:
' 
' 1. The origin of this software must not be misrepresented; you must not
' claim that you wrote the original software. If you use this software
' in a product, an acknowledgment in the product documentation would be
' appreciated but is not required.
' 2. Altered source versions must be plainly marked as such, and must not be
' misrepresented as being the original software.
' 3. This notice may not be removed or altered from any source distribution.

Strict

Import mojo
Import mnet

Function Main:Int()
  New TestApp()
  Return 0
End Function

Const PUBLISH_KEY := "pub-717f51f8-624b-44e1-a51c-952d6f54acf6"
Const SUBSCRIBE_KEY := "sub-4095405c-84b7-11e1-8c12-c79c9144291b"
Const CHANNEL_1:String = "monkey_lobby"
Const CHANNEL_2:String = "test_lobby"

Class TestApp Extends App
  Field scaleRatioX:Float = 1.0
  Field scaleRatioY:Float = 1.0
  Field translateX:Float = 0.0
  Field translateY:Float = 0.0

  Field pn:Pubnub = Null
  Field channel:PubnubChannel = Null
  Field msgSent:Bool = False
  Field ret:Int = 0
  Field msg:String
  Field pnStatus:String
  Field pnConnected:String
  Field currentChannel:String
  Field isMousePressed:Bool

  Method OnCreate:Int()
    currentChannel = "none"
    isMousePressed = False
    CheckForResolution()
    pn = New Pubnub()
    pn.Init( PUBLISH_KEY, SUBSCRIBE_KEY )
    SetUpdateRate(30)
    Return 0
  End Method

  Method OnUpdate:Int()
    If( KeyHit( KEY_ESCAPE ) )
      Error ""
    End If
    
    Local mx:Float = Float( MouseX() ) / scaleRatioX - translateX / scaleRatioX
    Local my:Float = Float( MouseY() ) / scaleRatioY - translateY / scaleRatioY

    'Check for clicks!
    If( TouchHit( 0 ) And Not isMousePressed )
      isMousePressed = True
      If( mx >= 10 And mx <= 220 And my >= 60 And my <= 90 )
        'Subscribe to channel 1
        If( pn )
          pn.Subscribe( CHANNEL_1 )
          currentChannel = CHANNEL_1
        End If
      Else If( mx >= 10 And mx <= 220 And my >= 100 And my <= 130 )
        'Subscribe to channel 2
        If( pn )
          pn.Subscribe( CHANNEL_2 )
          currentChannel = CHANNEL_2
        End If
      Else If( mx >= 10 And mx <= 110 And my >= 140 And my <= 170 )
        'Send "Hello"
        If( channel <> Null )
          If( channel.Connected() )
            channel.Send( "Hello" )
          End If
        End If
      Else If( mx >= 120 And mx <= 220 And my >= 140 And my <= 170 )
        'Send "World"
        If( channel <> Null )
          If( channel.Connected() )
            channel.Send( "World" )
          End If
        End If
      End If
    Else If( Not TouchHit( 0 ) And isMousePressed )
      isMousePressed = False
    End If
    
    If( pn )
      channel = pn.GetChannel()
    End If

    If( pn And channel <> Null )
      If( channel.MessageAvailable() > 0 )
        msg = channel.NextMessage()
      End If
    End If
    Return 0
  End Method

  Method OnRender:Int()
    PushMatrix()
    Translate( translateX, translateY )
    Scale( scaleRatioX, scaleRatioY )

    Cls()

    DrawText( "MNet Pubnub Example", 10, 10 )

    CheckForConnection()
    
    DrawText( "Pubnub status: " + pnStatus, 10, 20 )
    DrawText( "Pubnub channel: " + currentChannel, 10, 30 )
    DrawText( "Channel status: " + pnConnected, 10, 40 )

    ' Render "buttons"
    SetColor( 0, 255, 0 )
    DrawRect( 10, 60, 210, 30 )
    DrawText( "Subscribe to channel 1", 20, 70 )
    DrawRect( 10, 100, 210, 30 )
    DrawText( "Subscribe to channel 2", 20, 110 )

    SetColor( 255, 255, 0 )
    DrawRect( 10, 140, 100, 30 )
    DrawText( "Send 'Hello'", 20, 150 )
    DrawRect( 120, 140, 100, 30 )
    DrawText( "Send 'World'", 130, 150 )

    DrawRect( 10, 180, 210, 30 )
    DrawText( "Received: " + msg, 20, 190 )
    PopMatrix()

    Return 0
  End Method
  
  Method CheckForConnection:Void()
    If( pn <> Null )
      pnStatus = "running"
    Else
      pnStatus = "stopped"
    End If
    If( channel <> Null )
      If( channel.Connected() )
        pnConnected = "connected"
      Else
        pnConnected = "not connected"
      End If
    Else
      pnConnected = "no channel"
    End If
  End Method

  Method CheckForResolution:Void()
    scaleRatioX = 1.0
    scaleRatioY = 1.0
    translateX = 0.0
    translateY = 0.0
    Local fixWidth:Float = 320.0
    Local fixHeight:Float = 480.0
    'Aspect ratio
    Local dWidth:Float = Float( DeviceWidth() )
    Local dHeight:Float = Float( DeviceHeight() )
    Local aspectRatio:Float = dHeight / dWidth
    If( aspectRatio > 1.5 )
      scaleRatioX = dWidth / fixWidth
      scaleRatioY = dWidth / fixWidth
      translateY = ( dHeight - fixHeight * scaleRatioY ) / 2.0
    Else If( aspectRatio <= 1.5 )
      scaleRatioX = dHeight / fixHeight
      scaleRatioY = dHeight / fixHeight
      translateX = ( dWidth - fixWidth * scaleRatioX ) / 2.0
    Else
      scaleRatioX = dWidth / fixWidth
      scaleRatioY = dHeight / fixHeight
    End If
  End Method

End Class
